package com.itheima.project.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.basic.ResponseWrap;
import com.itheima.project.core.enums.dict.DataDictEnum;
import com.itheima.project.face.DataDictFace;
import com.itheima.project.utils.ResponseWrapBuild;
import com.itheima.project.vo.dict.DataDictVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName DataDictController.java
 * @Description 数字字典controller
 */
@Slf4j
@RestController
@RequestMapping("data-dict")
@Api(tags = "数字字典controller")
public class DataDictController {

    @Autowired
    DataDictFace dataDictFace;

    /***
     * @description 查询数据字典列表
     * @param dataDictVo 查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return: com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.itheima.restkeeper.req.DataDictVo>
     */
    @PostMapping("page/{pageNum}/{pageSize}")
    @ApiOperation(value = "查询字典分页",notes = "查询字典分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dataDictVo",value = "字典查询对象",required = false,dataType = "DataDictVo"),
        @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
        @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
    })
    ResponseWrap<Page<DataDictVo>> findDataDictVoPage(
        @RequestBody DataDictVo dataDictVo,
        @PathVariable("pageNum") int pageNum,
        @PathVariable("pageSize") int pageSize) {
        Page<DataDictVo> dataDictVoPage = dataDictFace.findDataDictVoPage(dataDictVo, pageNum, pageSize);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,dataDictVoPage);
    }

    /**
     * @Description 保存字典数据
     * @return
     */
    @PostMapping
    @ApiOperation(value = "添加字典",notes = "添加字典")
    @ApiImplicitParam(name = "dataDictVo",value = "字典信息",required = true,dataType = "DataDictVo")
    public ResponseWrap<DataDictVo> saveDataDict(@RequestBody DataDictVo dataDictVo) {
        DataDictVo dataDictVoResult = dataDictFace.saveDataDict(dataDictVo);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,dataDictVoResult);
    }

    /**
     * @Description 修改字典数据
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "修改字典",notes = "修改典")
    @ApiImplicitParam(name = "dataDictVo",value = "字典信息",required = true,dataType = "DataDictVo")
    public ResponseWrap<DataDictVo> updateDataDict(@RequestBody DataDictVo dataDictVo) {
        DataDictVo dataDictVoResult = dataDictFace.updateDataDict(dataDictVo);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,dataDictVoResult);
    }

    /**
     * @Description 根据parentKey获取value
     * @return
     */
    @GetMapping("parent-key/{parentKey}")
    @ApiOperation(value = "根据parentKey获取value",notes = "根据parentKey获取value")
    @ApiImplicitParam(paramType = "path",name = "parentKey",value = "字典parentKey",example = "URGE_TYPE",dataType = "String")
    ResponseWrap<List<DataDictVo>> findDataDictVoByParentKey(@PathVariable("parentKey") String parentKey) {
        List<DataDictVo> list = dataDictFace.findDataDictVoByParentKey(parentKey);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,list);
    }

    /**
     * @Description 根据dataKey获取DataDictVo
     * @return
     */
    @GetMapping("data-key/{dataKey}")
    @ApiOperation(value = "根据dataKey获取DataDictVo",notes = "根据dataKey获取DataDictVo")
    @ApiImplicitParam(paramType = "path",name = "dataKey",value = "字典dataKey",example = "URGE_TYPE",dataType = "String")
    ResponseWrap<DataDictVo> findDataDictVoByDataKey(@PathVariable("dataKey")String dataKey){
        DataDictVo dataDictVo = dataDictFace.findDataDictVoByDataKey(dataKey);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,dataDictVo);
    }

    @PostMapping("update-dataDict-enableFlag")
    @ApiOperation(value = "修改数字字典状态",notes = "修改数字字典状态")
    @ApiImplicitParam(name = "dataDictVo",value = "字典信息",required = true,dataType = "DataDictVo")
    ResponseWrap<DataDictVo> updateDataDictEnableFlag(@RequestBody DataDictVo dataDictVo) {
        DataDictVo dataDictVoResult = dataDictFace.updateDataDict(dataDictVo);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,dataDictVoResult);
    }

    /**
     * @Description 根据dataKeys获取DataDictVo
     * @return
     */
    @PostMapping("findValueByDataKeys")
    @ApiOperation(value = "根据dataKeys获取DataDictVo集合",notes = "根据dataKeys获取DataDictVo集合")
    @ApiImplicitParam(paramType = "body",name = "dataKeys",value = "字典dataKeys",example = "[URGE_TYPE]",dataType = "List")
    ResponseWrap<List<DataDictVo>> findValueByDataKeys(@RequestBody ArrayList<String> dataKeys){
        List<DataDictVo> dataDictVoList = dataDictFace.findValueByDataKeys(dataKeys);
        return ResponseWrapBuild.build(DataDictEnum.SUCCEED,dataDictVoList);
    }

}

